package com.example.demo.Controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.Dao.FormDao;
import com.example.demo.domin.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class FormController {

    @Autowired
    FormDao formDao;


    @RequestMapping(value = "/addForm",method = RequestMethod.POST)
    public String addForm(@RequestBody Form form) {//传来的数据转换为对象json->obj
        System.out.println("*****Form******:" + form);
        formDao.save(form);
        return null;
    }


    @RequestMapping(value = "deleteForm",method = RequestMethod.DELETE)
    public String deleteForm(Long id){
        System.out.println("*****ID******:" + id);
        formDao.deleteById(id);
        return null;
    }


    @RequestMapping(value = "getForms",method = RequestMethod.GET)
    public String getForms(){
        List<Form> forms = formDao.getForms();
        System.out.println("Forms:" + forms);
        String res= JSON.toJSONString(forms);
        System.out.println("Forms:" + res);
        return res;
    }


    @RequestMapping(value = "getFormById",method = RequestMethod.GET)
    public String getFormById(Long id){
        System.out.println("ID:" + id);
        Form form = formDao.getOne(id);
        System.out.println("Form:" + form);
        String res= JSON.toJSONString(form);
        System.out.println("Form:" + res);
        return res;
    }


    @RequestMapping(value = "editForm",method = RequestMethod.POST)
    public int editForm(@RequestBody Form form){
        System.out.println("*****Form******:" + form);
        formDao.save(form);
        return 1;
    }

    @RequestMapping(value = "collectForm",method = RequestMethod.GET)
    public void collectForm(Long id){
        System.out.println("ID:" + id);
        Form form = formDao.getOne(id);
        System.out.println("Form:" + form);
        System.out.println("State:" + form.isState());
        if (form.isState()==false){
            form.setState(true);
            System.out.println("State:" + form.isState());
        }
        else if(form.isState()==true){
            form.setState(false);
            System.out.println("State:" + form.isState());
        }
        System.out.println("Form:" + form);
        formDao.save(form);
    }

    @RequestMapping(value = "collectedForm",method = RequestMethod.GET)
    public String getCollectedForm(){
        List<Form> forms = formDao.getCollectedForm();
        System.out.println("Forms:" + forms);
        String res= JSON.toJSONString(forms);
        System.out.println("Forms:" + res);
        return res;
    }

}
